package sz.szsmk.citizencard.mvp.presenter;

import android.text.TextUtils;

import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.AccessTokenRequest;
import sz.szsmk.citizencard.model.network.bean.request.CollecQueryReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.CollecQueryRes;
import sz.szsmk.citizencard.mvp.contract.ICollectionContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.HttpApi;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/13 18:03
 * description:
 */
public class CollectionPresenter extends IBasePresenter
        implements ICollectionContact.ICollectionPresenter<ICollectionContact.ICollectionView> {
    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public CollectionPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void queryCollect(String mLatitude, String mLongitude) {
        CollecQueryReq tokenRequest = new CollecQueryReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        //tokenRequest.setLatitude("32.040456");
        //tokenRequest.setLongitude("118.72873");
        tokenRequest.setLatitude(TextUtils.isEmpty(mLatitude) ? "" : mLatitude);
        tokenRequest.setLongitude(TextUtils.isEmpty(mLongitude) ? "" : mLongitude);
        tokenRequest.setAccess_token(access_token);
        HttpApi httpApi = mNetworkDateSource.getHttpApi();
        mNetworkDateSource.queryCollect(tokenRequest, new SubscribeResult<CollecQueryRes>() {
            @Override
            protected void onOk(CollecQueryRes response) {
                List<CollecQueryRes.DataBean> data = response.getData();
                if (data != null) {
                    ((ICollectionContact.ICollectionView) mView).queryCollectSucc(data);
                }
            }

            @Override
            protected void onNext(CollecQueryRes response) {
                super.onNext(response);
                ((ICollectionContact.ICollectionView) mView).queryCollectFail();
            }

            @Override
            protected void onFailure(CollecQueryRes response) {
                super.onFailure(response);
                ((ICollectionContact.ICollectionView) mView).queryCollectFail();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

                ((ICollectionContact.ICollectionView) mView).queryCollectFail();
            }
        });
    }

    @Override
    public void queryUserCollectCity() {
        AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        accessTokenRequest.setAccess_token(access_token);
        mNetworkDateSource.queryUserCollectCity(accessTokenRequest, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {

            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

}
